const Fun = {
  is: {
    iscard: function (str) {
      let re = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!re.test(str)) {
        return false;
      }
      return true;
    },
    isemail: function (str) {
      let re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
      if (!re.test(str)) {
        return false;
      }
      return true;
    },
    isphone: function (str) {
      let re = /^1\d{10}$/;
      if (!re.test(str)) {
        return false;
      }
      return true;
    },
    isCarNumber: function (str) {
      let re =
        /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/;
      if (!re.test(str)) {
        return false;
      }
      return true;
    },
  },
  time: {
    getTime: function (timeType) {
      let now = new Date();
      let year = now.getFullYear(),
        month = now.getMonth() + 1,
        date = now.getDate(),
        hour = now.getHours(),
        minute = now.getMinutes(),
        second = now.getSeconds();
      minute = Fun.time.checkTime(minute);
      month = Fun.time.checkTime(month);
      date = Fun.time.checkTime(date);
      hour = Fun.time.checkTime(hour);
      second = Fun.time.checkTime(second);
      if (timeType == "yyyy-mm-dd") return year + "-" + month + "-" + date;
      else if (timeType == "mm-dd") return month + "-" + date;
      else if (timeType == "dd") return date;
      else return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    },
    checkTime: function (i) {
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    },
  },
  url: {
    queryString: function (key) {
      var globalCache = {};
      if (!globalCache["url_queryString"]) {
        globalCache["url_queryString"] = {};
      }
      if (!globalCache["url_queryString"][key]) {
        var url = window.location.search;
        var oRegex = new RegExp("[?&]" + key + "=([^&]+)", "i");
        var oMatch = oRegex.exec(url);
        if (oMatch && oMatch.length > 1) {
          var val = decodeURI(oMatch[1]);
          globalCache["url_queryString"][key] = val;
          return val;
        } else {
          return "";
        }
      }
      return globalCache["url_queryString"][key];
    },
  },
  scroll: {
    scrollTop: function () {
      document.body.scrollTop = document.documentElement.scrollTop = 0;
    },
  },
  cook: {
    setcookie: function (key, value, time = 1) {
      const d = new Date();
      d.setTime(d.getTime() + 3600 * 1000 * 24 * time);
      time = d.toUTCString();
      document.cookie = `${key}=${value};expires=${time}`;
    },
    getcookie: function (key) {
      let cookieArr = document.cookie.split(";");
      for (let i = 0; i < cookieArr.length; i++) {
        let tmpArr = cookieArr[i].split("=");
        let tmpKey = tmpArr[0].replace(/\s/g, "");
        if (tmpKey == key) {
          return tmpArr[1];
        }
      }
      return "";
    },
    removecookie: function (key) {
      Fun.cook.setcookie(key, 1, -1);
    },
    clearCookie: function () {
      let keys = document.cookie.match(/[^ =;]+(?=\=)/g);
      if (keys) {
        for (let i = keys.length; i--; ) {
          document.cookie = keys[i] + "=0;path=/;expires=" + new Date(0).toUTCString();
        }
      }
    },
  },
  runGood: {
    // 节流  throttle(function(){},)
    throttle: function (callback, wait) {
      let start = 0;
      return function (e) {
        let now = Date.now();
        if (now - start >= wait) {
          callback.call(this, e);
          start = now;
        }
      };
    },
    //防抖
    debounce: function (callback, time) {
      let timeId = null;
      return function (e) {
        if (timeId !== null) {
          clearTimeout(timeId);
        }
        timeId = setTimeout(() => {
          callback.call(this, e);
          timeId = null;
        }, time);
      };
    },
  },
};
export default Fun;
